//
//  ReportViewController.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/16.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "ReportViewController.h"
#import "chooseBtnView.h"
@interface ReportViewController ()
{
    NSArray *_titleArr;
}
@property (nonatomic,strong)UIView *reportView;//举报View
@property (nonatomic,strong)NSMutableArray *chooseArr;//选择Arr
@property (nonatomic,strong)UIButton *saveBtn;//举报按钮
@end

@implementation ReportViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTitleWithTitle:@"举报"];
    [self initLeftBarButtonWithImage:@"back"];
    _chooseArr = [NSMutableArray new];
    [self postReportLabelList];
//    titleArr = @[@"淫秽色情",@"人生攻击",@"有意加害",@"违法信心"];
}

-(UIView *)reportView//举报View
{
    if (!_reportView) {
        _reportView = [[UIView alloc]initWithFrame:CGRectMake(0, 10*screenW_6, SCREENW, 50*screenW_6*(_titleArr.count/2))];
        _reportView.backgroundColor = [UIColor whiteColor];
        
        //循环创建按钮
        for (int i = 0; i<_titleArr.count; i++) {
            float x = 15*screenW_6 + SCREENW/2 *(i%2)-(30*screenW_6*(i%2));
            float y = 50*screenW_6 *(i/2);
            NSString *intStr = [NSString stringWithFormat:@"%d",i];
            chooseBtnView *chooseBtn = [[chooseBtnView alloc]initWithFrame:CGRectMake(x, y,SCREENW/3, 49*screenW_6) Title:_titleArr[i][@"labelName"] NomolImageV:@"Forumunselect" SelectImageV:@"Forumselect" IsSelect:[_chooseArr containsObject:intStr]?YES:NO];
            chooseBtn.isChoose = [_chooseArr containsObject:intStr]?YES:NO;
            [_reportView addSubview:chooseBtn];
            chooseBtn.userInteractionEnabled = YES;
            chooseBtn.tag = i;
            UITapGestureRecognizer *chooseTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(chooseTap:)];
            [chooseBtn addGestureRecognizer:chooseTap];
            if (i/2==1) {
                [_reportView addSubview:[ToolViewVC addCellLineLab:CGRectMake(15*screenW_6, 49*screenW_6, SCREENW-15*screenW_6, 1*screenW_6)]];
            }
        }
    }
    return _reportView;
}

//举报选择点击事件
-(void)chooseTap:(UITapGestureRecognizer *)tap
{
    chooseBtnView *btn = (id)tap.view;
    btn.isChoose = !btn.isChoose;
    if (btn.isChoose) {//添加选中的
        [_chooseArr addObject:[NSString stringWithFormat:@"%d",(int)tap.view.tag]];
    }else
    {
        [_chooseArr removeObject:[NSString stringWithFormat:@"%d",(int)tap.view.tag]];
    }
    [btn IsChoose:btn.isChoose];//改变状态
}

-(UIButton *)saveBtn//举报按钮
{
    if (!_saveBtn) {
        _saveBtn = [TomamaUI createButton:UIButtonTypeCustom Frame:CGRectMake(10*screenW_6,CGRectGetMaxY(self.reportView.frame)+30*screenW_6, SCREENW-20*screenW_6, 45*screenW_6) targ:self sel:@selector(saveSelectBtn) titleColor:[UIColor whiteColor] font:[UIFont systemFontOfSize:17*screenW_6] title:@"举报" image:@"" backGroundImage:@""];
        _saveBtn.backgroundColor = [UIColor colorWithHexString:@"#59cbb3"];
        [_saveBtn.layer setCornerRadius:5*screenW_6];
        _saveBtn.layer.masksToBounds = YES;
    }
    return _saveBtn;
}
-(void)saveSelectBtn//举报点击事件
{
    if (_chooseArr.count == 0) {
        [TomamaUI creatAlertWithTarget:self message:@"至少选择一个举报理由" timeInterval:2];
        return;
    }
    NSString *labelId = @"";
    for (int i = 0; i < _chooseArr.count; i++) {
        if (i == 0) {
            labelId = _titleArr[[_chooseArr[i] integerValue]][@"id"];
        }else{
            labelId = [NSString stringWithFormat:@"%@,%@",labelId,_titleArr[[_chooseArr[i] integerValue]][@"id"]];
        }
    }
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/post/addPostReportRecord" Dic:@{@"jdPost.id":_postId,@"jdUser.id":[Storage getToken],@"labelId":labelId} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                if ([responseDic[@"success"] boolValue]) {
                    [TomamaUI creatAlertWithTarget:self message:@"举报成功" timeInterval:2 block:^{
                        [self.navigationController popViewControllerAnimated:YES];
                    }];
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
            });
        }];
    });
}

-(void)postReportLabelList
{
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/post/postReportLabelList" Dic:@{} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                if ([responseDic[@"success"] boolValue]) {
                    _titleArr = responseDic[@"data"];
                    [self.view addSubview:self.reportView];
                    [self.view addSubview:self.saveBtn];
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
            });
        }];
    });
}


@end
